<?php

namespace App\Controller\Admin;

use App\Agent\ChatAgent;
use App\Constants\RedisKey;
use Exception;
use Hyperf\Di\Annotation\Inject;
use App\Controller\InsideService\GameUserInside;
use App\Middleware\AdminMiddleware;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Utils\ApplicationContext;
use Hyperf\WebSocketClient\ClientFactory;

#[Controller(prefix: '/admin/Chat'), Middleware(AdminMiddleware::class)]
class ChatController extends BaseController {
    /**
     * @Inject
     * @var ClientFactory
     */
    protected ClientFactory $clientFactory;

    /**
     * 获取列表
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $page = $this->request->query('page', 1);
        $game_hash = $this->request->query('game_hash', '');

        $allMsg = [];
        $count = 0;
        if ($game_hash) {
            $chatRedis = (ApplicationContext::getContainer())->get(ChatAgent::class);
            $chatRoom = md5($game_hash . 0);
            $start = ($page - 1) * $limit;
            $end = $page * $limit - 1;
            $redisKey = RedisKey::GM_CHAT_PRE . ':History:' . $chatRoom;
            $allMsgOrigin = $chatRedis->lRange($redisKey, $start, $end);
            $count = $chatRedis->lLen($redisKey);
            foreach ($allMsgOrigin as $m) {
                $allMsg[] = json_decode($m, true);
            }
        }

        return $this->buildSuccess([
            'list'  => $allMsg,
            'count' => $count,
        ]);
    }

    /**
     * 禁言用户
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'mute')]
    public function mute(): array {
        $userDataArr = $this->uInfo();
        $openid = $this->request->query('openid', '');
        $game_hash = $this->request->query('game_hash', '');
        $mute = $this->request->query('mute', 0);

        return (new GameUserInside())->setCode($userDataArr['connection_code'])->mute([
            'openid'    => $openid,
            'game_hash' => $game_hash,
            'mute'      => (int)$mute,
        ]);
    }

    /**
     * 添加系统消息
     * @return array
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        $game_hash = $this->request->post('game_hash', '');
        $message = $this->request->post('message', '');

        if (!$game_hash) {
            return $this->buildFailed(-1, '请先选择游戏', []);
        }
        if (!$message) {
            return $this->buildFailed(-1, '请先填写消息内容', []);
        }
        $msg = [
            'user_info'      => [
                'nickname'  => '系统消息',
                'game_hash' => $game_hash,
            ],
            'type'           => 0,
            'item_id'        => 0,
            'equipment_info' => [],
            'message'        => $message
        ];
        $host = env('CHAT_HOST', '172.17.16.4') . ':' . env('CHAT_PORT', 8080) . '?User-Token=System';
        $client = $this->clientFactory->create($host);
        $client->push(json_encode($msg));

        return $this->buildSuccess();
    }

}